package com.shwread.android.ui.customview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shwread.android.qysw10000182.R;

import java.util.List;
import java.util.Map.Entry;

import logic.bean.OrderDetailColum;
import logic.util.MoneyUtil;
import logic.util.PicassoUtil;
import logic.util.Utils;


/**
 * @project qysw_e-commerce
 * @author xuzhijun
 * @date 2015年3月24日
 */
public class GoodsAdapter
        extends
        BaseAdapter
{
	
	private ViewHolder             holder;
	//	private OrderDetailInfo       data;
	private Context                context;
	private List<OrderDetailColum> goodsList;
	private StringBuilder          skuString;
	
	public GoodsAdapter(Context context, List<OrderDetailColum> data) {
		this.context = context;
		goodsList = data;
		
	}
	
	@Override
	public int getCount() {
		return goodsList == null ? 0 : goodsList.size();
	}
	
	@Override
	public Object getItem(int position) {
		return goodsList == null ? null : goodsList.get(position);
	}
	
	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null)
		{
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.goods_detail_item, parent, false);
			holder.name = (TextView) convertView.findViewById(R.id.goods_detail_item_name_tv);
			holder.count = (TextView) convertView.findViewById(R.id.goods_detail_item_original_count_tv);
			holder.price = (TextView) convertView.findViewById(R.id.goods_detail_item_original_price_tv);
			holder.classify = (TextView) convertView.findViewById(R.id.goods_detail_item_classify_tv);
			holder.coverIv = (ImageView) convertView.findViewById(R.id.goods_detail_item_image);
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		
		skuString = new StringBuilder();
		if (goodsList.get(position).getSkuProp() != null && goodsList.get(position).getSkuProp().size() > 0)
		{
			for (Entry<String, String> entry : goodsList.get(position).getSkuProp().entrySet())
			{
				if (skuString.length() > 0)
				{
					skuString.append(",");
				}
				skuString.append(entry.getKey() + ": " + entry.getValue());
			}
		}
		holder.name.setText(goodsList.get(position).getTitle());
		holder.count.setText("x" + goodsList.get(position).getBuyNum());
		holder.price.setText(" ¥" + Utils.getMoneyFormat(MoneyUtil.divide(goodsList.get(position).getSalePrice(), 100)));
		holder.classify.setText(skuString.toString());
		//判断URL是否为空
		if (!Utils.isEmpty(goodsList.get(position).getCoverUrl()))
        {
//			holder.coverIv.setImageUrl(goodsList.get(position).getCoverUrl());
			PicassoUtil.load(context, goodsList.get(position).getCoverUrl(),R.drawable.bg_small_image, holder.coverIv);
        }
		return convertView;
	}
	
	private class ViewHolder
	{
		public ImageView coverIv;
		public TextView       name;
		public TextView       price;
		public TextView       count;
		public TextView       classify;
	}
	
}
